package com.ece.gui;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JPanel;

import com.ece.beans.items.Item;

public class CartPanel extends JPanel{
	private final JList itemList = new JList();

	public CartPanel() {
		this.setSize(400, 400);
		this.setLayout(new BorderLayout());

		itemList.setFixedCellHeight(20);
		itemList.setFixedCellWidth(233);
		itemList.setVisibleRowCount(20);
		DefaultListModel listModel = new DefaultListModel();
		//listModel.setSize(10);
		itemList.setModel(listModel);

		this.add(itemList, BorderLayout.NORTH);
	}

	public void setItemList(List<Item> list) {
		this.remove(itemList);
		DefaultListModel listModel = new DefaultListModel();
		for(Item item : list)
			listModel.addElement(item);
		itemList.setModel(listModel);
		this.add(itemList);
		this.repaint();
	}

	public List<Item> getItemListSelected() {
		List<Item> itl=new ArrayList<Item>();
		for(Object o : this.itemList.getSelectedValues())
			itl.add((Item)o);

		return itl;
	}

	public void addItemList(List<Item> list) {
		//this.remove(itemList);
		DefaultListModel listModel = (DefaultListModel) itemList.getModel();
		for(Item item : list)
			listModel.addElement(item);
		itemList.setModel(listModel);
		this.add(itemList);
		this.repaint();
	}

	public void RemoveItemFromCart(List<Item> list){
		DefaultListModel listModel = (DefaultListModel) itemList.getModel();
		for(Item item : list)
			listModel.removeElement(item);
	}

	public List<Item> getCart() {
		List<Item> result = new ArrayList<Item>();
		DefaultListModel listModel = (DefaultListModel) itemList.getModel();
		for(int i = 0; i < listModel.size(); i++)
			result.add((Item)listModel.get(i));
		return result;
	}
}
